<?php
namespace Admin\Controller;
use Think\Controller;
class RoleController extends BaseController 
{
    public function add()
    {
    	if(IS_POST)
    	{
//			dump($_POST);exit();
    		$model = D('Role');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
		//取出所有的权限
		$priModel = D('privilege');
		$priData = $priModel->select();
//		dump($priData);exit();
		// 设置页面中的信息
		$this->assign(array(
			'priData' =>$priData,
			'_page_title' => '添加角色',
			'_page_btn_name' => '角色列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }

    /**
     * @name
     * @desc
     * @auther ycp
     */
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Role');
//    		var_dump(I('post.'));exit();
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst'));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
        $role_name = D('Role')->find($id);
		//取出所有的权限
		$priModel = D('privilege');
		$priData = $priModel->getTree();
    	$this->assign('priData', $priData);
    	//取出用户权限
        $user_priData = $priModel->getPriByUserId($id);
        $this->assign('user_priData', $user_priData);


        $this->assign('role_name', $role_name);
//        var_dump($priData,$user_priData);exit();
        // 设置页面中的信息
		$this->assign(array(
			'_page_title' => '修改角色',
			'_page_btn_name' => '角色列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function delete()
    {
    	$model = D('Role');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {
    	$model = D('Role');
    	$data = $model->search();
//		dump($data['data']);exit();
    	$this->assign(array(
    		'data' => $data['data'],
    		'page' => $data['page'],
    	));

		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '角色列表',
			'_page_btn_name' => '添加角色',
			'_page_btn_link' => U('add'),
		));
    	$this->display();
    }
}